//
//  ForumViewController.m
//  兼职页面
//
//  Created by kuiyuan liang on 2021/5/22.
//

#import "ForumViewController.h"
#import "CommonUtil.h"
#import "MessageView.h"
#import "TabTitleView.h"
#import "LotteryView.h"
#import "CommunityModel.h"
#import "CommunityTableViewCell.h"
#import "ForumHeaderView.h"
#import "TabTitleView.h"
#import "RuleTableViewCell.h"
#import "RuleTableViewCell.h"
#import <MJRefresh/MJRefresh.h>
#import "HomeHeaderView.h"
#import "DrawPrizeModel.h"
#import "PingTeTableViewCell.h"
#import "PingTeBottomView.h"
#import "ReplayViewController.h"
#import "GangCaiTableViewCell.h"
#import "ForumCustomNav.h"

@interface ForumViewController ()<UITableViewDataSource,UITableViewDelegate>


@property (strong, nonatomic) ForumCustomNav * customNav;

@property (copy, nonatomic) NSString * currentTitle;
@property (strong, nonatomic) UIView * addBtnView;
@property (strong, nonatomic) UIButton * addBtn;

@property (strong, nonatomic) NSMutableArray * models;
@property (strong, nonatomic) UITableView * tableView;
@property (strong, nonatomic) ForumHeaderView * headerView;
@property (strong, nonatomic) HomeHeaderView * homeHeaderView;
@property (assign, nonatomic) NSInteger currentIndex;
@property (strong, nonatomic) PingTeBottomView * bottomView;
@end

@implementation ForumViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.currentTitle = @"高手论坛";
    self.view.backgroundColor = RGB(243.0, 243.0, 243.0);
    
    
    
    if (self.type == PageTypeForum) {
        self.bottomView.height = 0;
        [self.view addSubview:self.tableView];
        [self.view addSubview:self.addBtnView];
        [self.view addSubview:self.addBtn];
        self.customNav.nameLabel.text = @"用户主页";
    }else if (self.type == PageTypeHomePage){
        [self.view addSubview:self.tableView];
        [self.view addSubview:self.bottomView];
        self.customNav.nameLabel.text = @"高手论坛";
    }
    [self.view addSubview:self.customNav];
    [self testData];
  
    
}


- (void)searchBtnClick:(UIButton *)sender{
    NSLog(@"搜索页面");
}


- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = false;
}


- (void)addBtnClick:(UIButton *)sender{
    NSLog(@"发帖");
    [self presentViewController:[ReplayViewController new] animated:YES completion:nil];
}

- (void)headerRefresh{
    [self.tableView.mj_header endRefreshing];
}


// 高手论坛
- (void)testData{
    self.models = [NSMutableArray array];
    // 模拟数据
    for (NSInteger i = 0; i < 10; i++) {
        CommunityModel *model = [[CommunityModel alloc] init];
        [self.models addObject:model];
        model.content = @"这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容";
        if (i == 0 || i == 5) {
            model.imageCount = 4;
            model.showjingpin = YES;
        }else if (i == 6){
            model.imageCount = 3;
        }
    }
    [self.tableView reloadData];
    
    [CommonUtil setupPlaceholderView:self.tableView models:self.models];
    
}



- (void)backBtnClick:(UIButton *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)wordBtnClick:(UIButton *)sender{
    NSLog(@"留言");
}

- (void)blacklistBtnClick:(UIButton *)sender{
    NSLog(@"黑名单");
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否将该用户添加到黑名单,添加后将不可对您进行回帖，留言，红包，等操作" preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *conform = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"点击了确认按钮");
        }];
        //2.2 取消按钮
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了取消按钮");
    }];
 
    //3.将动作按钮 添加到控制器中
    [alert addAction:conform];
    [alert addAction:cancel];
    
    //4.显示弹框
    [self presentViewController:alert animated:YES completion:nil];
    
}

#pragma mark - UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (self.type == PageTypeHomePage) {
        if ([self.currentTitle isEqualToString:@"港彩名人堂"] || [self.currentTitle isEqualToString:@"澳彩名人堂"]) {
            return 10;
        }else{
            return self.models.count;
        }
    }else if(self.type == PageTypeForum){
        if (self.currentIndex == 5) {
            return 1;
        }else{
            return self.models.count;
        }
    }
    return 0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.type == PageTypeHomePage) {
        
        if ([self.currentTitle isEqualToString:@"港彩名人堂"] || [self.currentTitle isEqualToString:@"澳彩名人堂"]) {
            GangCaiTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mingrencell"];
            return cell;
        }else{

        }
    }else{
        if (self.currentIndex == 5) {
            return [tableView dequeueReusableCellWithIdentifier:@"ruleCell"];;
        }
    }
    CommunityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.model = self.models[indexPath.row];
    cell.superVC = self;
    return cell;
}

#pragma mark - UITableViewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%zd",indexPath.row);
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.type == PageTypeHomePage) {
        if ([self.currentTitle isEqualToString:@"港彩名人堂"] || [self.currentTitle isEqualToString:@"澳彩名人堂"]) {
            return AD(125);
        }
    }else{
        if (self.currentIndex == 5) {
            return AD(500);
        }else{
 
        }
    }
    CommunityModel *model = self.models[indexPath.row];
    return model.cellHeight;
}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat pointY = scrollView.contentOffset.y;
    self.customNav.alpha = pointY/AD(250);
    
    if (pointY >= AD(210)) {
        [self.view addSubview:self.headerView.tabView];
        self.headerView.tabView.top = self.customNav.bottom;
    }else{
        [self.headerView addSubview:self.headerView.tabView];
        CGFloat y = self.headerView.messageView.bottom + AD(10);
        self.headerView.tabView.top = y;
    }
}


#pragma mark - lazy
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-self.bottomView.height)];
        [_tableView registerClass:[CommunityTableViewCell class] forCellReuseIdentifier:@"cell"];
        if (self.type == PageTypeHomePage) {
            [_tableView registerClass:[GangCaiTableViewCell class] forCellReuseIdentifier:@"mingrencell"];
        }else{
            [_tableView registerClass:[RuleTableViewCell class] forCellReuseIdentifier:@"ruleCell"];
        }
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        if (self.type == PageTypeForum) {
            _tableView.tableHeaderView = self.headerView;
        }else{
            _tableView.tableHeaderView = self.homeHeaderView;
        }
        
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
//iOS11系统及以上，
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
            // Fallback on earlier versions
        }
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
    }
    return _tableView;
}

-(ForumHeaderView *)headerView{
    if (!_headerView) {
        _headerView = [[ForumHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, AD(340))];
        [_headerView.backBtn addTarget:self action:@selector(backBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_headerView.searchBtn addTarget:self action:@selector(searchBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        __weak typeof(self) weakSelf = self;
        _headerView.tabView.actionBlock = ^(NSInteger index) {
            weakSelf.currentIndex = index;
            [weakSelf.tableView reloadData];
        };
        
    }
    return _headerView;
}

-(HomeHeaderView *)homeHeaderView{
    if (!_homeHeaderView) {
        _homeHeaderView = [[HomeHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, AD(380))];
        [_homeHeaderView.backBtn addTarget:self action:@selector(backBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_homeHeaderView.searchBtn addTarget:self action:@selector(searchBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        __weak typeof(self) weakSelf = self;
        _homeHeaderView.actionBlock = ^(NSString * _Nonnull title) {
            weakSelf.currentTitle = title;
            [weakSelf.tableView reloadData];
        };
        [_homeHeaderView.wordBtn addTarget:self action:@selector(wordBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_homeHeaderView.blacklistBtn addTarget:self action:@selector(blacklistBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _homeHeaderView;
}

-(UIView *)addBtnView{
    if (!_addBtnView) {
        _addBtnView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-AD(150), AD(60), AD(60))];
        _addBtnView.right = SCREEN_WIDTH - AD(50);
        _addBtnView.layer.cornerRadius = AD(30);
        _addBtnView.layer.shadowColor = [UIColor blackColor].CGColor;
        _addBtnView.layer.shadowRadius= AD(3);
        _addBtnView.backgroundColor = [UIColor whiteColor];
        _addBtnView.layer.shadowOpacity = 0.3f;
        _addBtnView.layer.shadowOffset = CGSizeMake(0,0);
    }
    return _addBtnView;
}

-(UIButton *)addBtn{
    if (!_addBtn) {
        _addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_addBtn setImage:[UIImage imageNamed:@"icon_add"] forState:UIControlStateNormal];
        [_addBtn addTarget:self action:@selector(addBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        _addBtn.layer.cornerRadius = AD(30);
        _addBtn.clipsToBounds = YES;
        _addBtn.frame = _addBtnView.frame;
        _addBtn.backgroundColor = [UIColor whiteColor];
    }
    return _addBtn;
}

-(PingTeBottomView *)bottomView{
    if (!_bottomView) {
        CGFloat h = kTabBarHeight + AD(20);
        _bottomView = [[PingTeBottomView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-h, SCREEN_WIDTH, h)];
    }
    return _bottomView;
}

-(ForumCustomNav *)customNav{
    if (!_customNav) {
        _customNav = [[ForumCustomNav alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, kStatusBarHeight+kNavBarHeight)];
        [_customNav.backBtn addTarget:self action:@selector(backBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _customNav;
}
@end
